<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->model( 'model_admin' );
		$this->load->library ( 'form_validation' );
		//$this->output->enable_profiler(true);
	}
	
	
	
	public function index()
	{	
		$this->load->view ( "admin/header" );
		$this->load->view ( "admin/index" );
		$this->load->view ( "admin/footer" );

	}
	
	
	/////////////////////////////** Prduct **///////////////////////////////////
	////////////////////////////////////////////////////////////////////////////
	
	
	public function add_product($error1="",$error2="",$error3="",$error4="",$error5="")
	{	
		
		$data['error1'] ="";
		$data['error2'] ="";
		$data['error3'] ="";
		$data['error4'] ="";
		$data['error5'] ="";
		if($error1!="") $data['error1'] =$error1;
		if($error2!="") $data['error2'] =$error2;
		if($error3!="") $data['error3'] =$error3;
		if($error4!="") $data['error4'] =$error4;
		if($error5!="") $data['error5'] =$error5;
		
		$data['product_cate'] = $this->model_admin->get_product_category();
		$data['form_type'] = $this->model_admin->get_form_type();
		$this->load->view ( "admin/header", $data );
		$this->load->view ( "admin/add_product", $data );
		$this->load->view ( "admin/footer", $data );

	}
	
	
	public function add_product_process()
	{	
	
	
		$this->form_validation->set_rules ( 'category[]', 'Category', 'trim|required' );
		$this->form_validation->set_rules ( 'pro_name', 'Product name', 'trim|required' );
		$this->form_validation->set_rules ( 'pro_size', 'Product size ', 'trim|required' );
		$this->form_validation->set_rules ( 'pro_detail', 'Product detail', 'trim|required' );
		$this->form_validation->set_rules ( 'pro_maker', 'Product maker', 'trim|required' );
		$this->form_validation->set_rules ( 'pro_time', 'Product time', 'trim|required' );
		$this->form_validation->set_rules ( 'pro_more', 'Peoduct more', 'trim' );
		$this->form_validation->set_rules ( 'minnimum[]', 'Product minnimum', 'trim|required' );
		$this->form_validation->set_rules ( 'price[]', 'Rate price', 'trim|required' );
		$this->form_validation->set_rules ( 'color[]', 'Color item', 'trim|required' );
		$this->form_validation->set_rules ( 'form_type', 'Form type', 'trim|required' );
		
	

		
		if ($this->form_validation->run () == FALSE) {
			$this->add_product ();
		}else{
			
			$temp1 = "";
			$temp2 = "";
			$temp3 = "";
			$temp4 = "";
			$temp5 = "";
			$error1 = "";
			$error2 = "";
			$error3 = "";
			$error4 = "";
			$error5 = "";
			$banner_img1 ="";
			$banner_img2 ="";
			$banner_img3 ="";
			$banner_img4 ="";
			$banner_img5 ="";
			$etc = str_pad(rand(1,995), 3, "0",STR_PAD_LEFT);
			
			
			if($_FILES["banner_img1"]["name"] != "" ){
				
				$etc_num = $etc;
				$config['upload_path'] = "img/uploads_product/";
				$config['allowed_types'] = "jpg|gif|png|jpeg";
				$config['max_height'] = 1024;
				$config['max_width'] = 1024;
				$config['overwrite'] = FALSE; 
				$config['file_name']  = 'pro'.date('Ymd_His').'_'.$etc_num.'.jpg';
				$this->load->library("upload");
				$this->upload->initialize($config);
				$temp1=$this->upload->do_upload("banner_img1");
				if(!$temp1) $error1 = $this->upload->display_errors();
				$this->upload->data();
				$file_name1 = $this->upload->file_name;
				
				if($temp1!="") //ถ้าอัพโหลดไม่มีปัญหา
				{
					$banner_img1 = $file_name1;	
				
				}
			

			}
			
			if($_FILES["banner_img2"]["name"] != "" ){
				
				$etc_num = $etc+1;
				$config['upload_path'] = "img/uploads_product/";
				$config['allowed_types'] = "jpg|gif|png|jpeg";
				$config['max_height'] = 1024;
				$config['max_width'] = 1024;
				$config['overwrite'] = FALSE; 
				$config['file_name']  = 'pro'.date('Ymd_His').'_'.$etc_num.'.jpg';
				$this->load->library("upload");
				$this->upload->initialize($config);
				$temp2=$this->upload->do_upload("banner_img2");
				if(!$temp2) $error2 = $this->upload->display_errors();
				$this->upload->data();
				$file_name2 = $this->upload->file_name;
				
				if($temp2!="") //ถ้าอัพโหลดไม่มีปัญหา
				{
					$banner_img2 = $file_name2;	
					//$this->thank_add ("slide_list");	  //แสดงค่า dataที่ได้
				}
		
			}
			
			if($_FILES["banner_img3"]["name"] != "" ){
				
				$etc_num = $etc+2;
				$config['upload_path'] = "img/uploads_product/";
				$config['allowed_types'] = "jpg|gif|png|jpeg";
				$config['max_height'] = 1024;
				$config['max_width'] = 1024;
				$config['overwrite'] = FALSE; 
				$config['file_name']  = 'pro'.date('Ymd_His').'_'.$etc_num.'.jpg';
				$this->load->library("upload");
				$this->upload->initialize($config);
				$temp3=$this->upload->do_upload("banner_img3");
				if(!$temp3) $error3 = $this->upload->display_errors();
				$this->upload->data();
				$file_name3 = $this->upload->file_name;
				
				if($temp3!="") //ถ้าอัพโหลดไม่มีปัญหา
				{
					$banner_img3 = $file_name3;	
					//$this->thank_add ("slide_list");	  //แสดงค่า dataที่ได้
				}
				
			}
			
			
			if($_FILES["banner_img4"]["name"] != "" ){
				
				$etc_num = $etc+3;
				$config['upload_path'] = "img/uploads_product/";
				$config['allowed_types'] = "jpg|gif|png|jpeg";
				$config['max_height'] = 1024;
				$config['max_width'] = 1024;
				$config['overwrite'] = FALSE; 
				$config['file_name']  = 'pro'.date('Ymd_His').'_'.$etc_num.'.jpg';
				$this->load->library("upload");
				$this->upload->initialize($config);
				$temp4=$this->upload->do_upload("banner_img4");
				if(!$temp4) $error4 = $this->upload->display_errors();
				$this->upload->data();
				$file_name4 = $this->upload->file_name;
				
				if($temp4!="") //ถ้าอัพโหลดไม่มีปัญหา
				{
					$banner_img4 = $file_name4;	
					//$this->thank_add ("slide_list");	  //แสดงค่า dataที่ได้
				}
				
			}
			
			
			if($_FILES["banner_img5"]["name"] != "" ){
				
				$etc_num = $etc+4;
				$config['upload_path'] = "img/uploads_product/";
				$config['allowed_types'] = "jpg|gif|png|jpeg";
				$config['max_height'] = 1024;
				$config['max_width'] = 1024;
				$config['overwrite'] = FALSE; 
				$config['file_name']  = 'pro'.date('Ymd_His').'_'.$etc_num.'.jpg';
				$this->load->library("upload");
				$this->upload->initialize($config);
				$temp5=$this->upload->do_upload("banner_img5");
				if(!$temp5) $error5 = $this->upload->display_errors();
				$this->upload->data();
				$file_name5 = $this->upload->file_name;
				
				if($temp5!="") //ถ้าอัพโหลดไม่มีปัญหา
				{
					$banner_img5 = $file_name4;	
					//$this->thank_add ("slide_list");	  //แสดงค่า dataที่ได้
				}
				
			}
			
			
				if($error1||$error2||$error3||$error4||$error5) //ถ้าอัพโหลดไม่มีปัญหา
				{
					$this->add_product ($error1,$error2,$error3,$error4,$error5);
				}else{
				
			
					$data = array (
						"pro_name" => $this->input->post ( 'pro_name' ),
						"pro_size" => $this->input->post ( 'pro_size' ),
						"pro_detail" => $this->input->post ( 'pro_detail' ),
						"pro_maker" => $this->input->post ( 'pro_maker' ),
						"pro_time" => $this->input->post ( 'pro_time' ),
						"pro_more" => $this->input->post ( 'pro_more' ),
						"form_type" => $this->input->post ( 'form_type' ),
						"banner_name1" => $_FILES["banner_img1"]["name"],
						"banner_name2" => $_FILES["banner_img2"]["name"],
						"banner_name3" => $_FILES["banner_img3"]["name"],
						"banner_name4" => $_FILES["banner_img4"]["name"],
						"banner_name5" => $_FILES["banner_img5"]["name"],
						"banner_img1" => $banner_img1,
						"banner_img2" => $banner_img2,
						"banner_img3" => $banner_img3,
						"banner_img4" => $banner_img4,
						"banner_img5" => $banner_img5
		
					);
					
					$data['category'] = $this->input->post ( 'category' );
					$data['category_name'] = $this->model_admin->select_category($this->input->post ( 'category' ));
					$data['minnimum'] = $this->input->post ( 'minnimum' );
					$data['price'] = $this->input->post ( 'price' );
					$data['color'] = $this->input->post ( 'color' );
					$data['product_no'] = $this->model_admin->get_product_max();
		
					$this->load->view ( "admin/header", $data );
					$this->load->view ( "admin/add_product_confirm", $data );
					$this->load->view ( "admin/footer", $data );
			
				}
				
		}

	}
	
	
	
	public function add_product_save()
	{	
	
		$this->form_validation->set_rules ( 'pro_no', 'pro_no', 'trim|required|unique[tbl_product.product_no]' );
		
		if ($this->form_validation->run () == FALSE) {
			echo "<meta charset='utf-8'><script>alert('Please check again');</script>";
			$this->add_product ();
		}else{
			
			 $this->model_admin->add_product();
			
		}
	
	
	}
	
	
	////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////
	
	
	/////////////////////////////** Category **//////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	
	
	public function category_list()
	{	
		
		$data['data_cate'] = $this->model_admin->get_category_list();
		$this->load->view ( "admin/header", $data );
		$this->load->view ( "admin/category_list", $data );
		$this->load->view ( "admin/footer", $data );

	}
	
	
	public function category_add()
	{	

		$this->load->view ( "admin/header");
		$this->load->view ( "admin/category_add");
		$this->load->view ( "admin/footer");
	}
	
	
	
	public function category_save()
	{
		
		$this->form_validation->set_rules ( 'cate_name', 'Category name', 'trim|required' );
		
		if ($this->form_validation->run () == FALSE) {
			$this->category_add ();
		}else{
			$this->model_admin->add_category();
		}
		
	}
	
	
	
	////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////
	
	
	
	/////////////////////////////** product rate **//////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	
	public function product_rate_list()
	{	
		
		$data['data_cate'] = $this->model_admin->get_pro_rate_list();
		$this->load->view ( "admin/header", $data );
		$this->load->view ( "admin/product_rate_list", $data );
		$this->load->view ( "admin/footer", $data );

	}
	
	
	public function product_rate_add()
	{	

		$this->load->view ( "admin/header");
		$this->load->view ( "admin/product_rate_add");
		$this->load->view ( "admin/footer");
	}
	
	
	
	public function product_rate_save()
	{
		
		$this->form_validation->set_rules ( 'pro_rate_name', 'Product rate name', 'trim|required' );
		$this->form_validation->set_rules ( 'pro_rate_min', 'Product rate min', 'trim|required|numeric' );
		$this->form_validation->set_rules ( 'pro_rate_max', 'Product rate max', 'trim|required}numeric' );
		
		if ($this->form_validation->run () == FALSE) {
			$this->product_rate_add ();
		}else{
			$this->model_admin->add_product_rate();
		}
		
	}
	
	////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////
	
	
	/////////////////////////////** Made date **//////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	
	public function made_date_list()
	{	
		
		$data['data_cate'] = $this->model_admin->get_made_date_list();
		$this->load->view ( "admin/header", $data );
		$this->load->view ( "admin/made_date_list", $data );
		$this->load->view ( "admin/footer", $data );

	}
	
	
	public function made_date_add()
	{	

		$this->load->view ( "admin/header");
		$this->load->view ( "admin/made_date_add");
		$this->load->view ( "admin/footer");
	}
	
	
	
	public function made_date_save()
	{
		
		$this->form_validation->set_rules ( 'made_date_name', 'Made date name', 'trim|required' );
		$this->form_validation->set_rules ( 'made_date', 'Made date', 'trim|required|numeric' );
		
		if ($this->form_validation->run () == FALSE) {
			$this->made_date_add ();
		}else{
			$this->model_admin->add_made_date();
		}
		
	}
	
	
	////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////.
	
	/////////////////////////////** product format **//////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	
	public function pro_format_list()
	{	
		
		$data['data_cate'] = $this->model_admin->get_pro_format_list();
		$this->load->view ( "admin/header", $data );
		$this->load->view ( "admin/pro_format_list", $data );
		$this->load->view ( "admin/footer", $data );

	}
	
	
	public function pro_format_add($error1="")
	{	
		$data['error1'] ="";
		if($error1!="") $data['error1'] =$error1;

		$this->load->view ( "admin/header",$data);
		$this->load->view ( "admin/pro_format_add",$data);
		$this->load->view ( "admin/footer",$data);
	}
	
	
	
	public function pro_format_save()
	{
		
		$this->form_validation->set_rules ( 'pro_format_name', 'Product format name', 'trim|required' );
		
		if ($this->form_validation->run () == FALSE) {
			$this->pro_format_add ();
		}else{
			
			if($_FILES["format_pic"]["name"] == "" ){
				if($_FILES["format_pic"]["name"] == "") $error1 = "please upload your picture";
				$this->pro_format_add ($error1);
				
			}else{
				
				$config['upload_path'] = "img/uploads_format/";
				$config['allowed_types'] = "jpg|gif|png|jpeg";
				$config['max_height'] = 1024;
				$config['max_width'] = 1024;
				$config['overwrite'] = FALSE; 
				$config['file_name']  = date('Ymd_His').'.jpg';
				$this->load->library("upload");
				$this->upload->initialize($config);
				$temp1=$this->upload->do_upload("format_pic");
				if(!$temp1) $error1 = $this->upload->display_errors();
				$this->upload->data();
				$file_name1 = $this->upload->file_name;
				
				if($temp1) //ถ้าอัพโหลดไม่มีปัญหา
				{
					$this->model_admin->add_pro_format ($file_name1);	
					//$this->thank_add ("slide_list");	  //แสดงค่า dataที่ได้
				}
				else  
				{
					$this->pro_format_add ($error1);
				}
			
				
			}
			
		}
		
	}
	
	
	////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////
	
	
	/////////////////////////////** product package **//////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	
	public function package_list()
	{	
		
		$data['data_cate'] = $this->model_admin->get_package_list();
		$this->load->view ( "admin/header", $data );
		$this->load->view ( "admin/package_list", $data );
		$this->load->view ( "admin/footer", $data );

	}
	
	
	public function package_add($error1="")
	{	
		
		$data['error1'] ="";
		if($error1!="") $data['error1'] =$error1;
		
		$this->load->view ( "admin/header",$data);
		$this->load->view ( "admin/package_add",$data);
		$this->load->view ( "admin/footer",$data);
	}
	
	
	
	public function package_save()
	{
		
		$this->form_validation->set_rules ( 'package_name', 'Package name', 'trim|required' );
		
		if ($this->form_validation->run () == FALSE) {
			$this->package_add ();
		}else{
			
			if($_FILES["package_pic"]["name"] == "" ){
				if($_FILES["package_pic"]["name"] == "") $error1 = "please upload your picture";
				$this->pro_format_add ($error1);
				
			}else{
				
				$config['upload_path'] = "img/uploads_package/";
				$config['allowed_types'] = "jpg|gif|png|jpeg";
				$config['max_height'] = 1024;
				$config['max_width'] = 1024;
				$config['overwrite'] = FALSE; 
				$config['file_name']  = date('Ymd_His').'.jpg';
				$this->load->library("upload");
				$this->upload->initialize($config);
				$temp1=$this->upload->do_upload("package_pic");
				if(!$temp1) $error1 = $this->upload->display_errors();
				$this->upload->data();
				$file_name1 = $this->upload->file_name;
				
				if($temp1) //ถ้าอัพโหลดไม่มีปัญหา
				{
					$this->model_admin->add_package ($file_name1);	
					//$this->thank_add ("slide_list");	  //แสดงค่า dataที่ได้
				}
				else  
				{
					$this->package_add ($error1);
				}
	
		}
		
	}
	
	}
	
	
	////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////
	
	/////////////////////////////** qty color rate **//////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	
	public function qty_color_rate_list()
	{	
		
		$data['data_cate'] = $this->model_admin->get_qty_color_rate_list();
		$this->load->view ( "admin/header", $data );
		$this->load->view ( "admin/qty_color_rate_list", $data );
		$this->load->view ( "admin/footer", $data );

	}
	
	
	public function qty_color_rate_add()
	{	

		$this->load->view ( "admin/header");
		$this->load->view ( "admin/qty_color_rate_add");
		$this->load->view ( "admin/footer");
	}
	
	
	
	public function qty_color_rate_save()
	{
		
		$this->form_validation->set_rules ( 'qty_color_rate_min', 'Qty color rate min', 'trim|required|numeric' );
		$this->form_validation->set_rules ( 'qty_color_rate_max', 'Qty color rate max', 'trim|required|numeric' );
		
		if ($this->form_validation->run () == FALSE) {
			$this->qty_color_rate_add ();
		}else{
			$this->model_admin->add_qty_color_rate();
		}
		
	}
	
	
	////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////.
	
	
	/////////////////////////////** qty color **//////////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////////
	
	public function qty_color_list($id="")
	{	

		if($id){
			
			$check = $this->model_admin->check_qty_color_list($id);
			
			if($check == true){
			
			$data['data_cate'] = $this->model_admin->get_qty_color_list($id);
			$this->load->view ( "admin/header", $data );
			$this->load->view ( "admin/qty_color_list", $data );
			$this->load->view ( "admin/footer", $data );
			
			}else{
				
			$this->qty_color_rate_list ();
				
			}
		}else{
			$this->qty_color_rate_list ();	
		}

	}
	
	
	public function qty_color_add()
	{	

		$this->load->view ( "admin/header");
		$this->load->view ( "admin/qty_color_rate_add");
		$this->load->view ( "admin/footer");
	}
	
	
	
	public function qty_color_save()
	{
		
		$this->form_validation->set_rules ( 'qty_color_rate_min', 'Qty color rate min', 'trim|required|numeric' );
		$this->form_validation->set_rules ( 'qty_color_rate_max', 'Qty color rate max', 'trim|required|numeric' );
		
		if ($this->form_validation->run () == FALSE) {
			$this->qty_color_rate_add ();
		}else{
			$this->model_admin->add_qty_color_rate();
		}
		
	}
	
	
	////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////.
	
	
	
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */